# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=font-noto
pkgver=0_git20211101
_gitrev=364fb14e20f82887280f103f638ff6450dbfc525
pkgrel=5
pkgdesc="Noto font families for Latin, Cyrillic and Greek scripts"
url="https://github.com/googlefonts/noto-fonts/"
arch="noarch"
license="OFL-1.1"
depends="fontconfig $pkgname-common=$pkgver-r$pkgrel"
makedepends="ruby"
checkdepends="cmd:xmllint"
source="https://github.com/googlefonts/noto-fonts/archive/$_gitrev/noto-fonts-$_gitrev.tar.gz
	45-noto-math.conf
	45-noto.xml.erb
	58-noto-math.conf
	58-noto.xml.erb
	noto-meta.json
	noto-meta
	"
builddir="$srcdir/noto-fonts-$_gitrev"

# List of subpackages generated by './noto-meta list-subpkgs'.
# NOTE: List is sorted by string length to prevent find doing partial matching.
_subpkgs="
	nyiakeng-puachue-hmong
	canadian-aboriginal
	indic-siyaq-numbers
	hanifi-rohingya
	mayan-numerals
	gunjala-gondi
	masaram-gondi
	mende-kikakui
	nastaliq-urdu
	meetei-mayek
	naskh-arabic
	pahawh-hmong
	rashi-hebrew
	sora-sompeng
	syloti-nagri
	kufi-arabic
	medefaidrin
	new-tai-lue
	pau-cin-hau
	signwriting
	warang-citi
	devanagari
	historical
	saurashtra
	bassa-vah
	malayalam
	mongolian
	samaritan
	sundanese
	armenian
	balinese
	buginese
	cherokee
	duployan
	ethiopic
	georgian
	gujarati
	gurmukhi
	javanese
	kayah-li
	ol-chiki
	tagbanwa
	tifinagh
	vithkuqi
	bengali
	elbasan
	grantha
	hanunoo
	kannada
	myanmar
	sharada
	sinhala
	soyombo
	symbols
	tibetan
	tirhuta
	arabic
	chakma
	coptic
	hebrew
	kaithi
	khojki
	lepcha
	rejang
	syriac
	telugu
	thaana
	wancho
	yezidi
	adlam
	bamum
	batak
	buhid
	khmer
	limbu
	music
	nushu
	oriya
	osage
	tamil
	ahom
	cham
	lisu
	math
	miao
	modi
	newa
	thai
	lao
	mro
	nko
	tai
	vai
	yi
	"
for _sub in $_subpkgs; do
	subpackages="$subpackages $pkgname-$_sub:_split"
done
subpackages="$subpackages
	font-croscore
	$pkgname-extra
	$pkgname-common
	$pkgname-all:_all
	"

_fontsdir='usr/share/fonts/noto'

build() {
	noto_meta gen-font-confs conf.d/
}

check() {
	xmllint --quiet --loaddtd --valid --nonet \
		--path /usr/share/xml/fontconfig/ conf.d/*.conf >/dev/null
}

package() {
	depends="$depends
		$pkgname-math=$pkgver-r$pkgrel
		$pkgname-symbols=$pkgver-r$pkgrel
		"

	install -D -m644 hinted/ttf/*/*.ttf -t "$pkgdir/$_fontsdir"/
	install -D -m644 conf.d/*.conf -t "$pkgdir"/etc/fonts/conf.avail/

	cd "$pkgdir"

	mkdir -p etc/fonts/conf.d
	local f; for f in etc/fonts/conf.avail/*.conf; do
		ln -s ../conf.avail/${f##*/} "$pkgdir"/etc/fonts/conf.d/${f##*/}
	done
}

_split() {
	pkgdesc=$(noto_meta pkgdesc $subpkgname)
	provides=""
	replaces=""

	# For backward compatibility (Alpine <3.16).
	case "${subpkgname#font-noto-}" in
		kayah-li) replaces="$pkgname-kayahli";;
		ol-chiki) replaces="$pkgname-olchiki";;
		historical) replaces="$pkgname-egyptianhieroglyphs $pkgname-oldturkic";;
	esac
	local name
	[ "$replaces" ] && for name in $replaces; do
		provides="$provides $name=$pkgver-r$pkgrel"
	done
	replaces="$replaces $pkgname<0_git20211101-r1 $pkgname-extra<0_git20211101-r1"

	local font; for font in $(noto_meta font-basenames $subpkgname); do
		amove "$_fontsdir"/$font-*.ttf
	done

	local fontconf="${subpkgname#font-}.conf"
	amove etc/fonts/conf.avail/[0-9]*-$fontconf
	amove etc/fonts/conf.d/[0-9]*-$fontconf
}

croscore() {
	pkgdesc="Chrome OS core fonts"
	provides="font-times"
	provider_priority=5

	local font; for font in Arimo Cousine Tinos; do
		amove "$_fontsdir"/$font*.ttf
	done
}

extra()	{
	pkgdesc="$pkgdesc (extra weights and variants)"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$pkgdir"
	mkdir -p "$builddir"/.tmp

	local font weight
	for font in $(noto_meta font-basenames '@'); do
		for weight in Regular Italic Bold BoldItalic; do
			mv "$_fontsdir"/$font-$weight.ttf "$builddir"/.tmp/ 2>/dev/null || true
		done
		amove "$_fontsdir"/$font-*.ttf
	done

	# Take advantage of the fact that this split function runs as the last
	# one (we don't count -all) and check if there are any files not listed
	# in fonts-meta.json
	if [ "$(ls -A "$_fontsdir")" ]; then
		error "Unknown files found in /$_fontsdir (update fonts-meta.json):"
		ls -1 "$_fontsdir" >&2
		return 1
	fi

	mkdir -p "$_fontsdir"
	mv "$builddir"/.tmp/*.ttf "$_fontsdir"/
}

common() {
	pkgdesc="Common files for Noto font families"
	depends=""

	amove etc/fonts/conf.avail/58-noto.conf
	amove etc/fonts/conf.d/58-noto.conf
}

_all() {
	pkgdesc="Google Noto font families that aim to support all the world's languages (meta package)"
	depends="$pkgname=$pkgver-r$pkgrel
		font-croscore=$pkgver-r$pkgrel
		$pkgname-extra=$pkgver-r$pkgrel
		"

	local subname; for subname in $_subpkgs; do
		depends="$depends $pkgname-$subname"
	done

	mkdir -p "$subpkgdir"
}

noto_meta() {
	"$srcdir"/noto-meta "$@"
}

sha512sums="
cb484098015a6847c1b2538635f5bc92e7fc50d1ffd9fbd406ae086b4c0dd8ae9a504ac6cf65320cc96c13a5b7462463466e72799a7facd116bcb092b9142452  noto-fonts-364fb14e20f82887280f103f638ff6450dbfc525.tar.gz
bbe805d212b0d46d4ce2d0fbc5a91d1f276a34f920b56ff6cb9474fdb3e1497bf9615cacfb9b69c04280ac4087c6f02a4fcb015b03c1ec0dd2d8180870811692  45-noto-math.conf
94c902aba178df855e388b1b2186866a0a28e6420f3f4ddbb798dfc7f551c13f93aa99c3ee7b112de218b817954b5f23c2cafdbd6a1828f2fec60ee61656e02a  45-noto.xml.erb
117830984239d10ce09bb79b7140b23810404ac13d44d1b3456587e34441273d9d4f4860fcabc19e8aec3ba3b60582c6b5411b28910e618fa2434968f83bd154  58-noto-math.conf
7024227b5b88f37bfff83792b36dbda66b27fdf472a0211c1b189a067d7cf7d406ad415e5a04ff1d7f067b9a4c9a97b275149062cb0ad65e7b0eed2835fa1c42  58-noto.xml.erb
e3979fdf5ff8326cf6533b60d370a6679818b91a2cda4e3fbf78985336a4aed9aff9031b23121862986188ab770bb60547f8a9b47911e9fb0c436e724a890956  noto-meta.json
f96d5a45af582ee7ddf8c6a9e6ae2b8266535c8443be4e49e667f1f5e15e5ecb93f20d4dd5dabbf19ccd41dbceff7b03a83b3ab5e6a44bde182795b3c95a2c91  noto-meta
"
